package com.aqie.meet.ui.my;

import android.content.Intent;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;

import com.aqie.libbase.event.EventManager;
import com.aqie.libbase.event.MessageEvent;
import com.aqie.libcommon.app.BaseActivity;
import com.aqie.libcommon.app.BaseApplication;
import com.aqie.libcommon.utils.SimpleJsonTool;
import com.aqie.libfactory.model.api.RspModel;
import com.aqie.libfactory.model.api.chat.ChatPermission;
import com.aqie.libfactory.model.db.User;
import com.aqie.libfactory.net.Network;
import com.aqie.libfactory.net.RemoteService;
import com.aqie.meet.R;
import com.aqie.meet.model.FriendPermission;
import com.aqie.meet.ui.login.AccountActivity;

import net.qiujuer.genius.ui.widget.Button;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 资料设置
 */
public class MaterialSettingActivity extends BaseActivity {


    @BindView(R.id.action_back)
    ImageView mImg;

    @BindView(R.id.s_v)
    Switch mSWitch;

    @BindView(R.id.delete_friend)
    Button mDelete;

    @BindView(R.id.friend_permission)
    ImageView friendPermission;

    @BindView(R.id.complain)
    ImageView complain;

    private String talentsId;

    private User user;
    private ChatPermission chatPermission = null;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_material_setting;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        EventManager.register(this);
        // 页面传参用人id
        Intent intent = getIntent();
        talentsId = intent.getStringExtra("talentsId");
        Log.e(TAG, "- " + talentsId);
        mSWitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                //控制开关字体颜色
                if (b && chatPermission.getBlacklist() != 1) {
                    Log.e(TAG, "open");
                    //BaseApplication.showToast("手动打开");
                    talentsBlacklist(talentsId, true);
                    mSWitch.setSwitchTextAppearance(getApplication(),R.style.s_true);
                }else if(!b && chatPermission.getBlacklist() != 0){
                    Log.e(TAG, "close");
                    // BaseApplication.showToast("手动关闭");
                    talentsBlacklist(talentsId, false);
                    mSWitch.setSwitchTextAppearance(getApplication(),R.style.s_false);
                }
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();

    }

    @Override
    protected void onResume() {
        super.onResume();
        getChatPermission(talentsId);
    }

    @OnClick(R.id.action_back)
    void back(){
        finish();
    }

    private void getChatPermission(String friendId) {
        RemoteService service = Network.remote();
        Call<RspModel<ChatPermission>> call = service.chatPermission(friendId);
        call.enqueue(new Callback<RspModel<ChatPermission>>() {
            @Override
            public void onResponse(Call<RspModel<ChatPermission>> call, Response<RspModel<ChatPermission>> response) {
                RspModel<ChatPermission> body = response.body();
                String rspString = SimpleJsonTool.toJson(body); Log.e(TAG, rspString);Log.e(TAG, friendId);

                if(response.code() == 401){
                    Log.e(TAG, "尚未登录");
                    AccountActivity.show(getApplication());
                    return;
                }
                if(body !=null && body.success()){
                    chatPermission = body.getData();
                    if(chatPermission.getBlacklist() == 1){
                        // BaseApplication.showToast("在黑名单");
                        mSWitch.setChecked(true);
                        mSWitch.setSwitchTextAppearance(getApplication(),R.style.s_true);
                    } else {
                        // BaseApplication.showToast("不在黑名单");
                        mSWitch.setChecked(false);
                        mSWitch.setSwitchTextAppearance(getApplication(),R.style.s_false);
                    }
                }
            }

            @Override
            public void onFailure(Call<RspModel<ChatPermission>> call, Throwable t) {

            }
        });
    }

    /**
     * 加入黑名单
     */
    private void talentsBlacklist(String talentsId, Boolean blacklist){
        RemoteService service = Network.remote();
        Call<RspModel<Boolean>> call = service.talentsBlacklist(talentsId, blacklist);
        call.enqueue(new Callback<RspModel<Boolean>>() {
            @Override
            public void onResponse(Call<RspModel<Boolean>> call, Response<RspModel<Boolean>> response) {

                RspModel<Boolean> body = response.body();
                if(response.code() == 401){
                    Log.e(TAG, "尚未登录");
                    AccountActivity.show(getApplication());
                    return;
                }
                if(body !=null && body.success()){
                    BaseApplication.showToast("操作成功");
                }else {
                    Log.e(TAG, "request fail");
                }
            }

            @Override
            public void onFailure(Call<RspModel<Boolean>> call, Throwable t) {
                Log.e(TAG, t.getMessage());
            }
        });
    }

    @OnClick(R.id.delete_friend)
    void deleteFriend(){
        RemoteService service = Network.remote();
        Call<RspModel<Boolean>> call = service.deleteFriend(talentsId);
        call.enqueue(new Callback<RspModel<Boolean>>() {
            @Override
            public void onResponse(Call<RspModel<Boolean>> call, Response<RspModel<Boolean>> response) {

                RspModel<Boolean> body = response.body();
                if(response.code() == 401){
                    Log.e(TAG, "尚未登录");
                    AccountActivity.show(getApplication());
                    return;
                }
                if(body !=null && body.success()){
                    BaseApplication.showToast("删除成功");
                }else {
                    Log.e(TAG, "request fail");
                }
            }

            @Override
            public void onFailure(Call<RspModel<Boolean>> call, Throwable t) {
                Log.e(TAG, t.getMessage());
            }
        });
    }

    /**
     * 跳转朋友权限
     */
    @OnClick(R.id.friend_permission)
    void friendPermission() {
        Intent intent = new Intent(this, FriendFindPermissionActivity.class);
        intent.putExtra("talentsId", talentsId);
        intent.putExtra("unLook", chatPermission.getUnLook());
        intent.putExtra("unGetLook", chatPermission.getUnGetLook());
        // Log.e(TAG, chatPermission.getUnLook() + " " + chatPermission.getUnGetLook());
        startActivity(intent);
    }

    @OnClick(R.id.complain)
    void complain(){
        Intent intent = new Intent(this, ComplainActivity.class);
        intent.putExtra("talentsId", talentsId);
        startActivity(intent);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent<FriendPermission> event) {
        switch (event.getType()) {
            case EventManager.EVENT_FRIEND_PERMISSION:
                Integer unLook = event.getData().getUnLook();
                Integer unGetLook = event.getData().getUnGetLook();
                // Log.e("FriendFindPermissionActivity", "接收到更新消息" + unLook + " " + unGetLook);
                chatPermission.setUnGetLook(unGetLook);
                chatPermission.setUnLook(unLook);
                break;
        }
    }
}